unit PBtnPngForm;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, PChildPngForm, StdCtrls, GDIPAPI, GDIPOBJ;

type
  TBtnPngForm = class(TChildPngForm)
    lblForm: TLabel;
    procedure lblFormMouseEnter(Sender: TObject);
    procedure lblFormMouseLeave(Sender: TObject);
    procedure lblFormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);virtual;
    procedure lblFormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    procedure lblFormClick(Sender: TObject);
  private
    xPos: Integer;
    yPos: Integer;
    mouseEntered: Boolean;
    imgNormal: HDC;
    imgHover: HDC;
    imgActive: HDC;
  public
    constructor Create(AOwner: TObject; idComponent: Integer; dirImageNormal, dirImageHover, dirImageActive: String; leftF, topF: Integer);
    procedure setParameters(AOwner: TObject; dirImage: HDC; leftF, topF: Integer);
    function getHDC(dirImage: String): HDC;
    function getImgNormal: HDC;
    function getImgHover: HDC;
    function getImgActive: HDC;
    function getXPos: Integer;
    function getYPos: Integer;
  end;

var
  BtnPngForm: TBtnPngForm;

implementation

uses PAuthorizationForm;

{$R *.dfm}

constructor TBtnPngForm.Create(AOwner: TObject; idComponent: Integer; dirImageNormal, dirImageHover, dirImageActive: String; leftF, topF: Integer);
begin
  inherited Create(AOwner, idComponent, dirImageNormal, leftF, topF);

  imgNormal := getHDC(dirImageNormal);
  imgHover := getHDC(dirImageHover);
  imgActive := getHDC(dirImageActive);

  xPos := leftF;
  yPos := topF;
end;

procedure TBtnPngForm.lblFormMouseEnter(Sender: TObject);
begin
  Self.setParameters(Self.getParentForm, imgHover, xPos, yPos);
  mouseEntered := True;
end;

procedure TBtnPngForm.setParameters(AOwner: TObject; dirImage: HDC; leftF, topF: Integer);
var
  screendc: HDC;
  pt1, pt2 : TPoint;
  sz : TSize;
  bf : TBlendFunction;
begin
  Self.BorderStyle := bsNone;
  if SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED) = 0 then
    ShowMessage(SysErrorMessage(GetLastError));

  backdc := dirImage;

  pt1 := Point(Left, Top);
  pt2 := Point(0,0);
  sz.cx := Width;
  sz.cy := Height;
  with bf do begin
    BlendOp := AC_SRC_OVER;
    BlendFlags := 0;
    SourceConstantAlpha := $FF;
    AlphaFormat := AC_SRC_ALPHA;
  end;

  UpdateLayeredWindow(Handle, screendc, @pt1, @sz, backdc, @pt2,0, @bf,ULW_ALPHA);
  ReleaseDC(0,screendc);

  Self.BoundsRect := GetRect(AOwner, leftF, topF, Width, Height);
end;

procedure TBtnPngForm.lblFormMouseLeave(Sender: TObject);
begin
  Self.setParameters(Self.getParentForm, imgNormal, xPos, yPos);
  mouseEntered := False;
end;

procedure TBtnPngForm.lblFormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  Self.setParameters(Self.getParentForm, imgActive, xPos, yPos);
//  Self.getParentForm.Handle
  Windows.SetFocus(TForm(Self.getParentForm).Handle);
end;

function TBtnPngForm.getHDC(dirImage: String): HDC;
var
  img: TGPBitmap;
  screendc: HDC;
begin
  if SetWindowLong(Handle, GWL_EXSTYLE, GetWindowLong(Handle, GWL_EXSTYLE) or WS_EX_LAYERED) = 0 then
    ShowMessage(SysErrorMessage(GetLastError));

  img := TGPBitmap.Create(dirImage,True);
  with img do begin
    GetHBITMAP(0,hbmp);
    Width := GetWidth;
    Height := GetHeight;
    Free;
  end;
  screendc := GetDC(0);
  backdc := CreateCompatibleDC(screendc);
  SelectObject(backdc, hbmp);

  Result := backdc;
end;

procedure TBtnPngForm.lblFormMouseUp(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
begin
  if mouseEntered then
    Self.setParameters(Self.getParentForm, imgHover, xPos, yPos)
  else
    Self.setParameters(Self.getParentForm, imgNormal, xPos, yPos);
end;

function TBtnPngForm.getImgActive: HDC;
begin
  Result := imgActive;
end;

function TBtnPngForm.getImgHover: HDC;
begin
  Result := imgHover;
end;

function TBtnPngForm.getImgNormal: HDC;
begin
  Result := imgNormal;
end;

function TBtnPngForm.getXPos: Integer;
begin
  Result := xPos;
end;

function TBtnPngForm.getYPos: Integer;
begin
  Result := yPos;
end;

procedure TBtnPngForm.lblFormClick(Sender: TObject);
begin
  inherited;
  //
end;

end.
